﻿using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;

namespace Helpers.Attributes
{
    public class ResponseCacheFilterFactory : Attribute, IFilterFactory
    {
        public bool IsReusable => true;

        public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
        {
            var endpoint = serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext?.GetEndpoint();
            var responseCacheOptions = serviceProvider.GetRequiredService<ApiCacheOptions>();
            return new ResponseCacheAttribute(responseCacheOptions);
        }
    }
}
